//
//  RequestManager.m
//  Leisure
//
//  Created by xalo on 16/6/29.
//  Copyright © 2016年 蓝鸥科技有限公司西安分公司. All rights reserved.
//

#import "RequestManager.h"


@implementation RequestManager

+ (void)fetchDataWithUrl:(NSString *)url parameter:(NSDictionary *)parameter method:(NSString *)method complete:(RequestComplete)complete updateUI:(UpdateUI)update{
    
    //创建请求对象,并设置请求对象的属性
    AFHTTPSessionManager *manager=[AFHTTPSessionManager manager];
    manager.responseSerializer=[AFHTTPResponseSerializer serializer];
    //子线程
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        if ([method isEqualToString:@"GET"]) {
            [manager GET:url parameters:parameter progress:^(NSProgress * _Nonnull downloadProgress) {
                
            } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                //把Data传出去
                complete(responseObject);
                //回到主线程
                dispatch_async(dispatch_get_main_queue(), ^{
                    update();
                });
            } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                
            }];
        }else{
            
            [manager POST:url parameters:parameter progress:^(NSProgress * _Nonnull uploadProgress) {
                
            } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                complete(responseObject);
                dispatch_async(dispatch_get_main_queue(), ^{
                    update();
                });
            } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                
            }];
        }
    });
}
@end
